package org.jfk.sys.role;

import java.math.BigDecimal;

import org.apache.log4j.Logger;
import org.jfk.core.controller.BaseController;
import org.jfk.core.utils.JsonUtil;
import org.jfk.sys.user.User;
import org.jfk.sys.user.UserRole;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.jfinal.kit.StrKit;

public class RoleController extends BaseController {
	private static Logger logger = Logger.getLogger(RoleController.class);
	
	public void edit() {
		setAttr("role", Role.dao.findById(getParaToInt()));
	}	


	public void update(){
		Role obj = getModel(Role.class);
		obj.update();
		super.renderJson();
	}	
	
	public void save() throws Exception {
		
		Role obj = getModel(Role.class);
		if (obj.get("id") == null){
			User currUser = super.getCurrUser();
			obj.set("creator", currUser.getInt("id"));			
			
			obj.save();
		}else{
			obj.update();
		}
		
		Integer roleId = obj.getInt("id");
		RoleUrl.dao.deleteByRoleId(roleId);
		String role_right = this.getPara("role_right");
		JsonArray array = JsonUtil.json2Array(role_right);
		for (int i = 0 ; array != null && i < array.size() ; i++){
			JsonElement el = array.get(i);
			JsonObject jo = el.getAsJsonObject();
			String pId = jo.get("pId").toString().replaceAll("\"", "");
			if(!"0".equals(pId) && StrKit.notBlank(pId) && !"null".equals(pId)){
				String urlId = jo.get("id").toString().replaceAll("\"", "");
				Integer urlId_ = new Integer(urlId);
				RoleUrl roleUrl = new RoleUrl();
				roleUrl.set("roleId", roleId);
				roleUrl.set("urlId", urlId_);
				User currUser = super.getCurrUser();
				roleUrl.set("creator", currUser.getInt("id").intValue());
				roleUrl.save();
			}
		}		
		super.returnSuc("操作成功！");
	}	
	
	public void list(){
		listColumns = new String[][] { 
				{ "id", "编号" }, 
				{ "roleName", "角色名称" },
				{ "description", "角色说明" },
				{ "createTime", "创建时间" }, 
				{ "creator_loginName", "创建人" }
				};
		super.setAttrs();
		
		setAttr("page",Role.dao.paginate(this));
	}	

	public void del() {
		String id = getPara("id");
		Role.dao.deleteById(id);
		super.renderText("test del");
	}

	
	public void delAll() {
		Integer[] checkedId = super.getParaValuesToInt("id[]");

		for (int i = 0; checkedId != null && i < checkedId.length; i++) {
			Role.dao.deleteById(checkedId[i]);
		}
		super.renderText("test delAll");
	}

	public void ajaxAddUserRole(){
		String username = super.getPara("username");
		int roleId = super.getParaToInt("roleId");
		User user = User.dao.findByLoginName(username);
		if (user != null){
			int userId = user.getInt("id").intValue();
			UserRole.dao.delUserRole(userId, roleId);

			UserRole userRole = new UserRole();
			userRole.set("userId", userId);
			userRole.set("roleId", roleId);
			userRole.save();
		}
		super.renderText("test");
	}

	public void ajaxDelUserRole(){
		int userId = super.getParaToInt("userId");
		int roleId = super.getParaToInt("roleId");
		UserRole.dao.delUserRole(userId, roleId);
		super.renderText("test");
	}	
}
